<?php

$user_obj = new user_obj();
$cash_obj = new cash_obj();
$lock_obj = new lock_obj();

if (isset($_REQUEST['action'])) {
  switch ($_REQUEST['action']) {
    case "add_entry":

      if ($lock_obj->isEntryLocked($_REQUEST['entry']['date'])) {
        error_obj::setError(new error_obj("locked cash", __FILE__, ''), PERIOD_IS_LOCKED);
      } else {
        $in_data = array();
        $other_in_data = array();
        $in_data['created'] = date("Y-m-d H:i:s");
        $in_data['who_added'] = $_SESSION['user']['user_id'];
        $in_data['user'] = $_REQUEST['entry']['user'];
        $in_data['date'] = base_obj::convertDateTo($_REQUEST['entry']['date']);
        $in_data['type_of_move'] = $_REQUEST['entry']['moveType'];
        $in_data['description'] = $_REQUEST['entry']['description'];
        if (isset($_REQUEST['entry']['s']))
          $in_data['s'] = $_REQUEST['entry']['s'];
        if (in_array($_REQUEST['entry']['moveType'], $GLOBALS['positiveMovesOfCash'])) {
          $in_data['amount'] = $_REQUEST['entry']['amount'];
        } elseif ($_REQUEST['entry']['moveType'] == $GLOBALS['bothMovesOfCash']) {
          $in_data['amount'] = -$_REQUEST['entry']['amount'];
          $in_data['transfer_between'] = time();
          $other_in_data = $in_data;
          $other_in_data['amount'] = $_REQUEST['entry']['amount'];
          $other_in_data['user'] = $_REQUEST['entry']['recipient'];
        } else {
          $in_data['amount'] = -$_REQUEST['entry']['amount'];
        }

        $res = $cash_obj->addEntry($in_data);
        if (error_obj::isError($res)) {
          error_obj::setError($res, UNEXCEPTED);
        } else {
          if (!empty($other_in_data)) {
            $res = $cash_obj->addEntry($other_in_data);
            if (error_obj::isError($res)) {
              error_obj::setError($res, UNEXCEPTED);
            }
          }
          base_obj::redirect(ROOT . '?input=state_of_cash');
        }
      }

      break;
  }
}

$filter = array();
$filter['active'] = 1;
$filter['client'] = 11;
$entry['recipients'] = $user_obj->getUsers('name, surname, users_id', $filter);
$entry['cash_types'] = $cash_obj->getCashMoveType();
$entry['bothMovesOfCash'] = $GLOBALS['bothMovesOfCash'];

$GLOBALS['template_data'] = $entry;
?>
